﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame1
{
    class Jogo2
    {
        Persona[] Bolas;
        //bool[] Vivo;
        bool apertado;

        public Jogo2(Texture2D xpto)
        {
            Bolas = new Persona[15];
           // Vivo = new bool[15];
            apertado = false;
            Random _r = new Random();
            for (int a = 0; a < 15; a++)
            {
                Bolas[a]= new Persona(_r.Next(1000), _r.Next(700), xpto);
                //Vivo[a] = true;
                for (int b = 0; b < a; b++)
                {
                    if (ColideIni(Bolas[a], Bolas[b]))
                    {
                        a--;
                    }
                }
            }
        }

        public void Reseta()
        {
            apertado = false;
            Random _r = new Random();
            for(int a = 0; a < 15; a++)
            {
                Bolas[a].Pos = new Vector2(_r.Next(1000), _r.Next(700));
                Bolas[a].Vivo = true;
                //Vivo[a] = true;
                for (int b = 0; b < a; b++)
                {
                    if (ColideIni(Bolas[a], Bolas[b]))
                    {
                        a--;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for(int a = 0;a<15;a++)
            {
                if (Bolas[a].Vivo == true)
                {
                    spriteBatch.Draw(Bolas[a].Img, Bolas[a].Pos, Color.Blue);
                }
            }
        }

        public char Update(MouseState ms, Tempo Time, Song[] Musicas)
        {
            if (Time.tempoRestante <= TimeSpan.FromSeconds(0))
            {
                MediaPlayer.Play(Musicas[2]);
                Time.tempoRestante = TimeSpan.FromSeconds(5);
                return '0';
            }
            if (ms.LeftButton == ButtonState.Pressed && apertado == false)
            {
                apertado = true;
                //estado.LeftButton = ButtonState.Pressed;
                if (Colide(ms, Time))
                {
                    Time.ConverteTempo();
                    Time.ResetaTempo();
                    return '3';
                }
            }
            if (ms.LeftButton == ButtonState.Released)
            {
                apertado = false;
            }
            return '2';
        }

        public bool Colide(MouseState ms, Tempo time)
        {
            Rectangle r1 = new Rectangle((int)ms.X, (int)ms.Y, 5, 5);
            for (int a = 0; a < 15; a++)
            {
                Rectangle r2 = new Rectangle((int)Bolas[a].Pos.X, (int)Bolas[a].Pos.Y, (int)Bolas[a].Img.Width, (int)Bolas[a].Img.Height);
                if (r1.Intersects(r2))
                {
                    Bolas[a].Vivo = false;
                    time.Ptos += 100;
                    return Verifica();
                }
            }
            return false;
        }
        public bool Verifica()
        {
            for (int a = 0; a < 15; a++)
            {
                if (Bolas[a].Vivo == true)
                {
                    return false;
                }
            }
            return true;
        }

        public bool ColideIni(Persona A, Persona B)
        {
            Rectangle r1 = new Rectangle((int)A.Pos.X,(int) A.Pos.Y, (int)A.Img.Width, (int)A.Img.Height);
            Rectangle r2 = new Rectangle((int)B.Pos.X,(int) B.Pos.Y, (int)B.Img.Width, (int)B.Img.Height);
            if (r1.Intersects(r2))
            {
                return true;
            }
            return false;
        }
    }
}
